package prueba;

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*; 

public class AnimatedSprite extends Sprite implements Runnable{

	int AnimationSleepTime;
	int Orientation;
	private final int OR_UP=1, OR_DOWN=2,OR_LEFT=4,OR_RIGHT=3;
	
	public AnimatedSprite(Image image,int frameWidth,int frameHeight,int AnimationSleepTime)
	{
		super(image,frameWidth,frameHeight);
		this.AnimationSleepTime=AnimationSleepTime;
	}

	public void run(){
		while(2>1)
		{
			if(this.getFrame()==this.getFrameSequenceLength()-1)
			{
				this.setFrame(0);
			}
			else
			{
				this.setFrame(this.getFrame()+1);
			}
			try { 
				Thread.sleep(AnimationSleepTime); 
			} catch (InterruptedException e) { 
				System.out.println(e.toString()); 
			}
		}
	}
	
	public void setOrientation(int newOrientation)
	{
		this.Orientation=newOrientation;
	}
	
	public int getOrientation()
	{
		return Orientation;
	}
	
	public void stepBack()
	{
		System.out.println("sta entrando..");
		System.out.println("X"+this.getX()+" Y="+this.getY());
		switch(Orientation)
		{
		case OR_UP:
			this.setPosition(this.getX(),this.getY()+1);
			System.out.println("X"+this.getX()+" Y="+this.getY());
			break;
		case OR_DOWN:
			this.setPosition(this.getX(),this.getY()-1);
			System.out.println("X"+this.getX()+" Y="+this.getY());
			break;
		case OR_LEFT:
			this.setPosition(this.getX()+1,this.getY());
			System.out.println("X"+this.getX()+" Y="+this.getY());
			break;
		case OR_RIGHT:
			this.setPosition(this.getX()-1,this.getY());
			System.out.println("X"+this.getX()+" Y="+this.getY());
			break;
		}
	}
}
